#include <iostream>
#include <string>
#include <cstdbool>
#include <ctime>
#include <cctype>

using namespace std;

const unsigned int MIX_Plus_and_Less= 500;  //可以通过修改该数值提高加减法范围
const unsigned int MAX_by_except = 100; //可以通过修改该数值提高乘除法范围
const unsigned int Judge_branch = 4;    //这个是四则运算选项，0+，1-，2x，3÷
bool Plus_Less ( unsigned int n_number_1, unsigned int n_number_2, int n);
bool By_except (unsigned int n_number_1,unsigned int n_number_2, int n);
bool Calculation(unsigned int n_number_1,unsigned int n_number_2);
long cin_yz(void);
int main(void) {
    unsigned int n_number_1 = 0;
    unsigned int n_number_2 = 0;
    unsigned int n_num = 0;
    char ch = 'n';
    clock_t Time_recording = clock() * CLOCKS_PER_SEC;
    string s_ShowFormula{"计算以下公式（一千以内加减法，100以内整数乘除法）"};
    srand((int) Time_recording);
    while (ch == 'n')
    {
        cout << s_ShowFormula << endl;
        while (Calculation(n_number_1, n_number_2) == true)
        {
            n_num++;
        }
        cout << "答题结束" << endl << "答对题目数量: " << n_num << endl;
        cout << "是否重新答题？(输入 n 键继续，其余按键都是结束)" << endl ;
        n_num = 0;
        while(getchar() != '\n')
            continue;
        cin >> ch;
    }
    cout << "结束" << endl;
    return 0;
}

bool Calculation(unsigned int n_number_1,unsigned int n_number_2)
{
    unsigned int temp = rand() % Judge_branch;
    switch (temp)
    {
        case 0 : n_number_1 = rand() % MIX_Plus_and_Less;
                 n_number_2 = rand() % MIX_Plus_and_Less;
                 if(Plus_Less(n_number_1 ,n_number_2 ,temp) == false)
                     return false;
            break;  //加法

        case 1 : n_number_1 = rand() % MIX_Plus_and_Less;
                 n_number_2 = rand() % MIX_Plus_and_Less;
                  if(Plus_Less(n_number_1 ,n_number_2 ,temp) == false)
                       return false;
            break;  //减法

        case 2 : n_number_1 = rand() % MAX_by_except;
                 n_number_2 = rand() % MAX_by_except;
                  if(By_except(n_number_1 ,n_number_2 ,temp) == false)
                      return false;
            break;  //乘法

        case 3 : n_number_1 = rand() % MAX_by_except;
                 n_number_2 = rand() % n_number_1;
                  if(By_except(n_number_1 ,n_number_2 ,temp) == false)
                     return false;
            break;  //除法
    }
    return true;
}


bool Plus_Less (unsigned int n_number_1,unsigned int n_number_2,int n)
{
    int temp = 0;
    if(n == 0) {
        cout << n_number_1 << " + " << n_number_2 << " = " ;
        temp=cin_yz();
            if((n_number_1 + n_number_2) ==  temp)
                return true;
    }
    else{
        cout << n_number_1 << " - " << n_number_2 << " = " ;
        temp=cin_yz();
        if((n_number_1 - n_number_2) == temp)
            return true;
    }
    return false;
}
bool By_except (unsigned int n_number_1,unsigned int n_number_2, int n)
{
    int temp = 0;
    long except_temp = 0;
    long except_temp_2 = 0;
    if(n == 2) {
        cout << n_number_1 << " x " << n_number_2 << " = " ;
        temp=cin_yz();
        if((n_number_1 * n_number_2) == temp)
            return true;
    }
    else{
        while(n_number_1 == 0 || n_number_2 == 0
              ||n_number_1 == 1 || n_number_2 == 1)
        {
            n_number_1 = rand() % MAX_by_except;
            n_number_2 = rand() % n_number_1;
        }
        except_temp = n_number_1 * n_number_2 ;
        cout << except_temp << " / " << n_number_2 << " = " ;
        except_temp_2 =(long)cin_yz();
        if((except_temp / n_number_2) == except_temp_2 )
            return true;
    }
    return false;
}
long cin_yz(void) {
    string s_temp;
    long n_temp = 0;
    int num = 0;
    cin >> s_temp;
    while (s_temp[num] != '\0')
    {
        if(s_temp[0] == '-')
        {
            num++;
            continue;
        }
        if (s_temp[num] < '0' || s_temp[num] > '9')
        {
            cout << "输入错误，重新输入" << endl;
            while (getchar() != '\n')
                continue;
            num = 0;
            cin >> s_temp;
            continue;
        }
        num++;
    }
    n_temp = stoi(s_temp);
    return n_temp;
}